<?php

include_once 'config.php';

// ===============================
// ===============================
// ===============================
// check functions
// ===============================
// ===============================
// ===============================

function hasFilledout($pid, $questionnaire) {
    global $DATABASE_NAME;

    //start session
    if (!isset($_SESSION)) {
        session_start();
    }

    $conn = new MongoClient(); // connect
    //get database
    $db = $conn->selectDB($DATABASE_NAME);

    $query = array('pid' => $pid);
    $cursor = $db->$questionnaire->find($query);

    //close connection
    $conn->close();

    //send back the result
    if ($cursor->count() > 0) {
        return true;
    }

    return false;
}

// ===============================
function hasPassedCheats($pid, $questionnaire) {
    global $DATABASE_NAME;
    global $CHEATS;
    $passed = true;

    $cheatQuestions = $CHEATS[$questionnaire];

    //start session
    if (!isset($_SESSION)) {
        session_start();
    }

    $conn = new MongoClient(); // connect
    //get database
    $db = $conn->selectDB($DATABASE_NAME);

    $query = array('pid' => $pid);
    $cursor = $db->$questionnaire->find($query);

    //close connection
    $conn->close();

    //send back the result
    if ($cursor->hasNext()) {
        $data = $cursor->getNext();
        $keys = array_keys($data["questions"]);
        foreach ($keys as $key) {
            if (in_array($key, $cheatQuestions)) {
                $passed = false;
                break;
            }
        }
    }

    return $passed;
}
